package com.example.wasaimovie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wasaimovie.Mapper.LeavingMsgMapper;
import com.example.wasaimovie.Model.entity.LeavingMsg;
import com.example.wasaimovie.Model.entity.User;
import com.example.wasaimovie.Service.LeavingMsgService;
import com.example.wasaimovie.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LeavingMsgServiceImpl extends ServiceImpl<LeavingMsgMapper, LeavingMsg> implements LeavingMsgService {

    @Autowired
    LeavingMsgService leavingMsgService;

    @Autowired
    UserService userService;

    @Value("${movie.image.url.path}")
    String url;

    /**
     * 获取留言和用户信息
     * @return 详细信息
     */
    @Override
    public List<Map<String, Object>> getLeavingMsgAndUser() {
        List<LeavingMsg> leavingMsgList = leavingMsgService.list();

        List<Map<String, Object>> result = new ArrayList<>();
        for (LeavingMsg leavingMsg : leavingMsgList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", leavingMsg.getId());
            map.put("leavingMessage", leavingMsg);
            // 根据uid查询用户信息
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("id", leavingMsg.getUid());
            User user = null;
            List<User> users = userService.list(qw);
            if (users.isEmpty()){
                user = new User();
                user.setNickname("用户已注销");
            }
            else{
                user = users.get(0);
            }
            user.setAvatar(url + user.getAvatar());
            map.put("user", user);
            result.add(map);
        }
        return result;
    }
}
